<!--  -->
<template>
  <div class="dialog-editor-banner-item">
    <el-dialog title="Banner图片编辑" :visible.sync="dialogFormVisible" width="600px" :modal-append-to-body="false">
      <el-form :model="form" v-if="dialogFormVisible" @submit.native.prevent>
        <el-form-item label="图片 alt 属性" :label-width="formLabelWidth" style="margin-bottom: 8px;">
          <el-input v-model="form.alt" autocomplete="off" size="mini"
            style="max-width:240px;"></el-input>
        </el-form-item>
        <el-form-item label="图片 title 属性" :label-width="formLabelWidth" style="margin-bottom: 8px;">
          <el-input v-model="form.title" autocomplete="off" size="mini"
            style="max-width:240px;"></el-input>
        </el-form-item>

        <el-form-item label="设置链接地址" :label-width="formLabelWidth" style="margin-bottom: 8px;">
          <el-form-item label="" style="margin-bottom: 8px;" :error="hrefErrorMessage">
            <div>
              <el-input :disabled="isNoLink" v-model="form.imglink.href" autocomplete="off" size="mini"
                style="max-width:240px;"></el-input>&nbsp;&nbsp;
              <el-checkbox v-model="isNoLink">不需要设置链接</el-checkbox>
            </div>
          </el-form-item>
          <el-form-item label="打开方式" style="margin-bottom: 8px;">
            <div>
              <el-radio-group :disabled="isNoLink" v-model="form.imglink.target" size="mini">
                <el-radio :label="'_self'">当前窗口</el-radio>
                <el-radio :label="'_blank'">新开窗口</el-radio>
              </el-radio-group>
            </div>
          </el-form-item>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    editorBannerItem: {
      type: Function
    }
  },
  data () {
    return {
      dialogFormVisible: false,
      form: {},
      formLabelWidth: '120px',
      theme: -1,
      // 是否不需要设置链接
      isNoLink: false,
      // 对应的编辑列表中的key位置
      index: null,
      // 校验链接错误信息
      hrefErrorMessage: null
    }
  },
  watch: {
    isNoLink: {
      handler (newValue) {
        if (newValue) {
          this.form.imglink = {
            href: '',
            // text: 'Link Text',
            target: '_blank'
          }
          this.hrefErrorMessage = null
        }
      },
      deep: true
    }
  },
  methods: {
    show (bannerItem, index, theme) {
      this.isNoLink = false
      this.dialogFormVisible = true
      this.form = bannerItem
      this.index = index
      this.theme = theme
      if (!this.form.imglink.href) {
        this.isNoLink = true
      }
    },
    submit () {
      this.hrefErrorMessage = null
      if (!this.form.imglink.href && !this.isNoLink) {
        // 需要输入链接的情况下，链接地址为空时
        this.hrefErrorMessage = '请输入链接地址！'
        return
      }
      // 对链接地址的验证
      let bool = /^(\/|\/\/|http:\/\/|https:\/\/).*?/.test(this.form.imglink.href)
      if (!bool && !this.isNoLink) {
        this.hrefErrorMessage = '请输入正确链接地址！例如：https://www.baidu.com/'
        return
      }
      this.editorBannerItem(this.index, this.form)
      this.close()
    },
    close () {
      this.dialogFormVisible = false
      this.form = {}
      this.index = null
      this.theme = -1
      this.hrefErrorMessage = null
      this.isNoLink = false
    }
  }
}

/**

form : {
        src: '/api/image/repertory/ea43241506e7d6fbec4be0c6e43aae18/eb96d0d4abe0187947d1d5a48ca240c9.gif',
        title: '',
        alt: '',
        content: '',
        link: {
          href: '#',
          text: 'Link Text',
          target: '_blank'
        },
        repertoryId: 89
      }
 *
 */
</script>
